package com.byt.framlib.commonutils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.byt.framlib.R;

import java.util.Timer;
import java.util.TimerTask;

public class MessageDialog extends Dialog {

    private Activity mActivity;
    private String titleName;
    private String content;
    private long closeTime = 3000;

    private TextView stv_message_title;
    private TextView stv_message_content;
    private ImageView img_dialog_msg_tips;
    private ImageView img_message_close;
    private Animation in;

    public long getCloseTime() {
        return closeTime;
    }
    private CloseDialogCallBack mCloseDialogCallBack;
    public void setCloseTime(long closeTime) {
        this.closeTime = closeTime;
        closeTimer();
        startTime();
    }

    /**
     * @param activity
     */
    public MessageDialog(Activity activity, String titleName, String content,CloseDialogCallBack closeDialogCallBack) {
        super(activity, R.style.dialog_message);
        this.mActivity = activity;
        this.titleName = titleName;
        this.content = content;
        this.mCloseDialogCallBack = closeDialogCallBack;
        in = AnimationUtils.loadAnimation(mActivity, R.anim.push_up_in);
    }

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.dialog_message, null);
        this.setContentView(layout);
        Window window = getWindow();
        //设置边框距离
        window.getDecorView().setPadding(0, 0, 0, 0);
        //设置dialog位置
        window.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置宽高
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = (int) mActivity.getResources().getDimension(R.dimen.y300);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.main_menu_animstyle); // 添加动画
        //设置点击Dialog外部任意区域关闭Dialog
        setCanceledOnTouchOutside(true);
        initWidget(layout);
    }

    private void initWidget(View view) {
        stv_message_title = view.findViewById(R.id.tv_dialog_title_msg);
        stv_message_content = view.findViewById(R.id.tv_dialog_content_msg);
        img_dialog_msg_tips = view.findViewById(R.id.img_dialog_msg_tips);
        img_message_close = view.findViewById(R.id.img_dialog_message_close);
        if (!TextUtils.isEmpty(titleName)) {
            stv_message_title.setText(titleName);
        }
        stv_message_content.setText(content);
        img_message_close.setOnClickListener(v -> {
            dismiss();
        });
        view.setOnClickListener(view1 -> {
            mCloseDialogCallBack.clickDialog();
            dismiss();
        });
        startTime();
    }

    public void addItem(String title, String content) {
        stv_message_title.setText(title);
        stv_message_content.setText(content);
        //设置显示时的动画
        TranslateAnimation mShowAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        mShowAction.setDuration(500);
        stv_message_title.startAnimation(mShowAction);
        stv_message_content.startAnimation(mShowAction);
        img_dialog_msg_tips.startAnimation(mShowAction);
        closeTimer();
        startTime();
    }

    @Override
    public void show() {
        super.show();
        startTime();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        closeTimer();
        mCloseDialogCallBack.closeDialog();
    }

    private Timer timer = null;

    /**
     * 启动定时器
     */
    public void startTime() {
        if (timer == null) {
            timer = new Timer();
        }
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                dismiss();
            }
        }, closeTime);
    }

    /**
     * 关闭定时器
     */
    public void closeTimer() {
        if (timer != null) {
            timer.cancel();
            timer.purge();
            timer = null;
        }
    }
    public interface CloseDialogCallBack{

        void closeDialog();

        void clickDialog();
    }
}